package com.logictech.activity.system.service.impl;

import com.logictech.activity.system.dao.SystemRoleDao;
import com.logictech.activity.system.dao.SystemUserDao;
import com.logictech.activity.system.entity.SystemUser;
import com.logictech.activity.system.entity.Menu;
import com.logictech.activity.system.entity.SystemRole;
import com.logictech.activity.system.service.SystemUserService;
import com.logictech.activity.system.vo.LoginUser;
import com.logictech.common.util.data.StringUtils;
import com.logictech.lims.service.LogService;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.jeecgframework.p3.core.utils.common.PageList;
import org.jeecgframework.p3.core.utils.common.PageQuery;
import org.jeecgframework.p3.core.utils.common.Pagenation;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("systemUserService")
public class SystemUserServiceImpl implements SystemUserService {
	@Resource
	private SystemUserDao systemUserDao;

	@Resource
	private LogService logService;

	@Resource
	private SystemRoleDao systemRoleDao;

	@Transactional(rollbackFor = { Exception.class })
	public void doAdd(SystemUser systemUser, List<String> roleIds) {
		this.systemUserDao.add(systemUser);
		if (roleIds != null) {
			List<SystemRole> systemRoleList = systemRoleDao.queryRoleList(roleIds);
			List<String> roleNameList = new ArrayList<String>();
			for (SystemRole systemRole : systemRoleList) {
				roleNameList.add(systemRole.getRoleName());
			}
			systemUser.setRoleLog("[" + StringUtils.listToString(roleNameList) + "]");
		} else {
			systemUser.setRoleLog("[]");
		}

		logService.doAdd("1", systemUser);

		if ((roleIds != null) && (roleIds.size() > 0)) {
			for (String roleId : roleIds) {
				this.systemUserDao.insertUserRoleRel(systemUser.getUserId(), roleId);
			}
		}

	}

	@Transactional(rollbackFor = { Exception.class })
	public void doEdit(SystemUser systemUser, List<String> roleIds) {
		this.systemUserDao.update(systemUser);

		if (roleIds != null) {
			List<SystemRole> systemRoleList = systemRoleDao.queryRoleList(roleIds);
			List<String> roleNameList = new ArrayList<String>();
			for (SystemRole systemRole : systemRoleList) {
				roleNameList.add(systemRole.getRoleName());
			}
			systemUser.setRoleLog("[" + StringUtils.listToString(roleNameList) + "]");
		} else {
			systemUser.setRoleLog("[]");
		}

		logService.doAdd("2", systemUser);
		this.systemUserDao.deleteRolesByUserId(systemUser.getUserId());
		if ((roleIds != null) && (roleIds.size() > 0)) {
			for (String roleId : roleIds) {
				this.systemUserDao.insertUserRoleRel(systemUser.getUserId(), roleId);
			}
		}
	}

	@Transactional(rollbackFor = { Exception.class })
	public void doDelete(Long id) {
		SystemUser systemUser = (SystemUser) this.systemUserDao.get(id);

		if ("admin".equals(systemUser.getUserId())) {
			throw new RuntimeException("admin用户不能删除");
		}
		List<String> roleIds = queryUserRoles(systemUser.getUserId());
		if (roleIds.size() > 0) {
			List<SystemRole> systemRoleList = systemRoleDao.queryRoleList(roleIds);
			List<String> roleNameList = new ArrayList<String>();
			for (SystemRole systemRole : systemRoleList) {
				roleNameList.add(systemRole.getRoleName());
			}
			systemUser.setRoleLog("[" + StringUtils.listToString(roleNameList) + "]");
		} else {
			systemUser.setRoleLog("[]");
		}

		logService.doAdd("3", systemUser);
		this.systemUserDao.deleteRolesByUserId(systemUser.getUserId());
		systemUser.setDeleteFlag("1");
		this.systemUserDao.update(systemUser);
	}

	public SystemUser queryById(Long id) {
		SystemUser systemUser = (SystemUser) this.systemUserDao.get(id);
		return systemUser;
	}

	public PageList<SystemUser> queryPageList(PageQuery<SystemUser> pageQuery) {
		PageList<SystemUser> result = new PageList<SystemUser>();
		Integer itemCount = this.systemUserDao.count(pageQuery);
		List<SystemUser> list = this.systemUserDao.queryPageList(pageQuery, itemCount);
		Pagenation pagenation = new Pagenation(pageQuery.getPageNo(), itemCount.intValue(), pageQuery.getPageSize());
		result.setPagenation(pagenation);
		result.setValues(list);
		return result;
	}

	public List<String> queryUserRoles(String userId) {
		return this.systemUserDao.queryUserRoles(userId);
	}

	public List<Menu> queryUserMenuAuth(List<String> roleIds) {
		return this.systemUserDao.queryUserMenuAuth(roleIds);
	}

	public LoginUser queryUserByUserId(String userId) {
		return this.systemUserDao.queryUserByUserId(userId);
	}

	@Override
	public void doEdit(SystemUser systemUser) {
		this.systemUserDao.update(systemUser);
	}

	@Override
	public List<SystemUser> queryAllList() {
		List<SystemUser> list = this.systemUserDao.queryAllList();
		return list;
	}
	
	@Override
	public List<SystemUser> queryNormalList() {
		List<SystemUser> list = this.systemUserDao.queryNormalList();
		return list;
	}

	@Override
	public List<SystemUser> queryLoginList() {
		List<SystemUser> list = this.systemUserDao.queryLoginList();
		return list;
	}
}
